##########################################################################################################
############### This code reproduces Figure 1 in the Manuscript ##########################################
##########################################################################################################

### setting working directory 

### loading libraries
library(dplyr)
library(ggplot2)
library(ggthemes)
library(arrow)
library(lubridate)
library(reshape2)
library(ggpubr)


##########################################################################################################
########################################## Preparing the data ############################################
##########################################################################################################

### Loading aggregated data showing number of comments per day for a given channel (e.g. channel-day observations). 
file <-  "data-replication/fig1_data.csv"
df_days <- read.csv(file)


### creating a dummy variable indicating whether comment activity is from before or after the ban
df_days$after <- 0
df_days$after[df_days$days >= 0] <- 1
df_days$after <- as.factor(df_days$after)

### creating an object for each Channel category
### creating dataframe for each channel type where each row is a day
df_days_pro <- df_days %>% filter(type =="Pro-Kremlin")
df_days_pro_b <- df_days %>% filter(type =="Pro-Kremlin (blocked)")
df_days_anti <- df_days %>% filter(type =="Regime-critical")
df_days_enter <- df_days %>% filter(type =="Entertainment")


##########################################################################################################
########################################## Simple OLS ############################################
##########################################################################################################

# simple lm for pro-Kremlin blocked
lm_pro_b_b1 <- lm(sum_comments ~ days*after, df_days_pro_b)
summary(lm_pro_b_b1)

# simple lm for entertainment 
lm_enter_1 <- lm(sum_comments ~ days*after, df_days_enter)
summary(lm_enter_1)

# simple lm for regime critical
lm_anti_1 <- lm(sum_comments ~ days*after, df_days_anti)
summary(lm_anti_1)

# simple lm for pro-Kremlin
lm_pro_1 <- lm(sum_comments ~ days*after, df_days_pro)
summary(lm_pro_1)

### storing the estimates 
df_days_pro_b$predlm = predict(lm_pro_b_b1)
df_days_enter$predlm = predict(lm_enter_1)
df_days_anti$predlm = predict(lm_anti_1)
df_days_pro$predlm = predict(lm_pro_1)


##########################################################################################################
########################################## Creating Figure 1 ############################################
##########################################################################################################

pro_kremlin_b <- ggplot(df_days_pro_b, aes(x = days, y = sum_comments, color = after)) + 
  geom_point(alpha = 0.8)+
  geom_smooth(aes(y = predlm), size = 1) +
  #  scale_color_brewer(NULL, type = 'qual', palette = 6) + 
  geom_vline(xintercept = 0, linetype = "dashed") + # 0 == 12th of March, the day after YouTube announces the ban
  geom_vline(xintercept = -8, linetype = "dashed", color = "red") + # -8 == 4th of March, the day Putin signs the anti-fake news law
  geom_vline(xintercept = -17, linetype = "dashed", color = "grey") + # -17 == 24th of February, the day of the invasion
  labs(x = "Days before and after the YouTube ban",
       y = "Number of comments", 
       color = "Time period") +
  stat_smooth(method = "lm", level = 0.95) +
  scale_colour_manual(values=c("light blue", "black"),
                      labels = c("Before the ban", "After the ban")) +
  theme(axis.text=element_text(size= 10),
        axis.title=element_text(size=15),
        strip.text = element_text(size = 14, face  = "bold")) +
  scale_x_continuous(limits = c(-40,40)) +
  scale_y_continuous(labels = scales::comma, limits = c(0, 260000)) +
  #scale_y_continuous(limits = c(0,14)) +
  annotate("rect", xmin = 0, xmax = 40, ymin = 0 ,ymax = 260000, alpha = 0.25, fill = "#8FBC8F") +
  facet_wrap(~type, nrow = 2) +
  theme_tufte() 

pro_kremlin <- ggplot(df_days_pro, aes(x = days, y = sum_comments, color = after)) + 
  geom_point(alpha = 0.8)+
  geom_smooth(aes(y = predlm), size = 1) +
  #  scale_color_brewer(NULL, type = 'qual', palette = 6) + 
  geom_vline(xintercept = 0, linetype = "dashed") + # 0 == 12th of March, the day after YouTube announces the ban
  geom_vline(xintercept = -8, linetype = "dashed", color = "red") + # -8 == 4th of March, the day Putin signs the anti-fake news law
  geom_vline(xintercept = -17, linetype = "dashed", color = "grey") + # -17 == 24th of February, the day of the invasion
  labs(x = "Days before and after the YouTube ban",
       y = "Number of comments", 
       color = "Time period") +
  stat_smooth(method = "lm", level = 0.95) +
  scale_colour_manual(values=c("light blue", "black"),
                      labels = c("Before the ban", "After the ban")) +
  theme(axis.text=element_text(size= 10),
        axis.title=element_text(size=15),
        strip.text = element_text(size = 14, face  = "bold")) +
  scale_x_continuous(limits = c(-40,40)) +
  scale_y_continuous(labels = scales::comma, limits = c(0,65000)) +
  #scale_y_continuous(limits = c(0,14)) +
  annotate("rect", xmin = 0, xmax = 40, ymin = 0 ,ymax = 65000, alpha = 0.25, fill = "#8FBC8F") +
  facet_wrap(~type, nrow = 2) +
  theme_tufte()

anti_kremlin <- ggplot(df_days_anti, aes(x = days, y = sum_comments, color = after)) + 
  geom_point(alpha = 0.8)+
  geom_smooth(aes(y = predlm), size = 1) +
  #  scale_color_brewer(NULL, type = 'qual', palette = 6) + 
  geom_vline(xintercept = 0, linetype = "dashed") + # 0 == 12th of March, the day after YouTube announces the ban
  geom_vline(xintercept = -8, linetype = "dashed", color = "red") + # -8 == 4th of March, the day Putin signs the anti-fake news law
  geom_vline(xintercept = -17, linetype = "dashed", color = "grey") + # -17 == 24th of February, the day of the invasion
  labs(x = "Days before and after the YouTube ban",
       y = "Number of comments", 
       color = "Time period") +
  stat_smooth(method = "lm", level = 0.95) +
  scale_colour_manual(values=c("light blue", "black"),
                      labels = c("Before the ban", "After the ban")) +
  theme(axis.text=element_text(size= 10),
        axis.title=element_text(size=15),
        strip.text = element_text(size = 14, face  = "bold")) +
  scale_x_continuous(limits = c(-40,40)) +
  scale_y_continuous(labels = scales::comma, limits = c(0, 65000)) +
  #scale_y_continuous(limits = c(0,14)) +
  annotate("rect", xmin = 0, xmax = 40, ymin = 0 ,ymax = 65000, alpha = 0.25, fill = "#8FBC8F") +
  facet_wrap(~type, nrow = 2) +
  theme_tufte() 

enter <- ggplot(df_days_enter, aes(x = days, y = sum_comments, color = after)) + 
  geom_point(alpha = 0.8)+
  geom_smooth(aes(y = predlm), size = 1) +
  #  scale_color_brewer(NULL, type = 'qual', palette = 6) + 
  geom_vline(xintercept = 0, linetype = "dashed") + # 0 == 12th of March, the day after YouTube announces the ban
  geom_vline(xintercept = -8, linetype = "dashed", color = "red") + # -8 == 4th of March, the day Putin signs the anti-fake news law
  geom_vline(xintercept = -17, linetype = "dashed", color = "grey") + # -17 == 24th of February, the day of the invasion
  labs(x = "Days before and after the YouTube ban",
       y = "Number of comments", 
       color = "Time period") +
  stat_smooth(method = "lm", level = 0.95) +
  scale_colour_manual(values=c("light blue", "black"),
                      labels = c("Before the ban", "After the ban")) +
  theme(axis.text=element_text(size= 10),
        axis.title=element_text(size=15),
        strip.text = element_text(size = 14, face  = "bold")) +
  scale_x_continuous(limits = c(-40,40)) +
  scale_y_continuous(labels = scales::comma, limits = c(0, 260000)) +
  #scale_y_continuous(limits = c(0,14)) +
  annotate("rect", xmin = 0, xmax = 40, ymin = 0 ,ymax = 260000, alpha = 0.25, fill = "#8FBC8F") +
  facet_wrap(~type, nrow = 2) +
  theme_tufte() 


combined_lm <- ggarrange(pro_kremlin, enter,
                         anti_kremlin, pro_kremlin_b,
                         ncol=2, nrow=2, common.legend = TRUE, legend="bottom")
dir.create("figures", showWarnings = FALSE)
ggsave("figures/figure-1.pdf", width = 30, height = 12, units = "cm")
